<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style></style>
</head>

<body>
    <canvas id="canvas" width="600" height="800" style="border: 1px solid #ccc"></canvas>
</body>

</html>
<script>
    let cns = document.querySelector('#canvas');
    let context = cns.getContext('2d');

    function creatPolygon(context, n, dx, dy, size) {
        // 开始一条新路径
        context.beginPath();
        // 算出每个点的角度
        let degree = (2 * Math.PI) / n;
        // 循环遍历，画出多边形
        for (let i = 0; i < n; i++) {
            let x = Math.cos(i * degree) * size + dx;
            let y = Math.sin(i * degree) * size + dy;
            context.lineTo(x, y);
        }
        // 关闭一条路径,使首尾点相连，不是结束路径的意思
        context.closePath();
    };

    creatPolygon(context, 3, 150, 150, 80);
    context.fillStyle = 'red';
    context.fill();

    creatPolygon(context, 3, 150, 350, 80);
    context.strokeStyle = 'blue';
    context.stroke();

    creatPolygon(context, 8, 150, 550, 80);
    context.fillStyle = 'hotpink';
    context.fill();

    // 画圆
    context.beginPath();
    context.arc(350, 150, 80, Math.PI / 180 * 0, Math.PI / 180 * -90, false);
    context.stroke();
    


</script>